package org.bonorsoft.common.dingapi;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiV2DepartmentGetRequest;
import com.dingtalk.api.request.OapiV2DepartmentListparentbydeptRequest;
import com.dingtalk.api.request.OapiV2DepartmentListsubRequest;
import com.dingtalk.api.response.OapiV2DepartmentGetResponse;
import com.dingtalk.api.response.OapiV2DepartmentListparentbydeptResponse;
import com.dingtalk.api.response.OapiV2DepartmentListsubResponse;
import com.taobao.api.ApiException;
import org.bonorsoft.common.constant.URLConstant;

public class DingdingDepart {

    /** 功能描述:根据部门id获取部门详细
     *
     * @vison: 1.0.0
     * @Author: WilliamYe
     * @Date: 2020/11/18 9:02
     */
    public static String getDepartment(Long id) throws Exception{
        DingTalkClient client = new DefaultDingTalkClient(URLConstant.GET_DEPARTMENT);
        OapiV2DepartmentGetRequest req = new OapiV2DepartmentGetRequest();
        req.setDeptId(id);
        req.setLanguage("zh_CN");
        OapiV2DepartmentGetResponse rsp = client.execute(req, AccessTokenUtil.getToken());
        JSONObject obj = JSON.parseObject(rsp.getBody());
        return obj.getString("result");
    }

    /** 功能描述:获取部门列表(包含家校通讯录-要排除【根据dept_id为-7】)
     *
     * @vison: 1.0.0
     * @Author: WilliamYe
     * @Date: 2020/11/18 9:13
     */
    public static String getDepartmentListSub(Long dept_id){
        String result = "";
        try {
            DingTalkClient client = new DefaultDingTalkClient(URLConstant.LISTSUB_DEPARTMENT);
            OapiV2DepartmentListsubRequest req = new OapiV2DepartmentListsubRequest();
            req.setDeptId(dept_id);
            OapiV2DepartmentListsubResponse rsp = client.execute(req, AccessTokenUtil.getToken());
            System.out.println(rsp.getBody());
            JSONObject obj = JSON.parseObject(rsp.getBody());
            result = obj.getString("result");
        } catch (ApiException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     *查询部门所有父部门列表
     * @param deptId 部门id
     * @return
     */
    public static String listParentByDept(long deptId) {
        try {
            DingTalkClient client = new DefaultDingTalkClient(URLConstant.LIST_PARENT_IDS);
            OapiV2DepartmentListparentbydeptRequest req = new OapiV2DepartmentListparentbydeptRequest();
            req.setDeptId(deptId);
            OapiV2DepartmentListparentbydeptResponse rsp = client.execute(req, AccessTokenUtil.getToken());
            System.out.println(rsp.getBody());
            JSONObject obj = JSON.parseObject(rsp.getBody());
            String result = obj.getString("result");
            JSONObject ids = JSON.parseObject(result);
            return ids.getString("parent_id_list")
                    .replace("[","")
                    .replace("]","");
        } catch (ApiException e) {
            e.printStackTrace();
            return "";
        }
    }
}
